using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class IncomeDetailsDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static DataSet GetAllByIncome(int IncomeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_GetAllByIncome", dbConn);
            dbCmd.Parameters.AddWithValue("@Income_ID", IncomeID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        //public static bool Delete(int _Income_ID)
        //{
        //    bool retVal = false;
        //    SqlConnection dbConn = SQLServerUtil.CreateConnection();
        //    SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_Delete", dbConn);
        //    dbCmd.Parameters.AddWithValue("@Income_ID", _Income_ID);
        //    dbCmd.CommandType = CommandType.StoredProcedure;
        //    try
        //    {
        //        dbConn.Open();
        //        if (dbCmd.ExecuteNonQuery() > 0)
        //        {
        //            retVal = true;
        //        }
        //    }
        //    finally
        //    {
        //        dbConn.Close();
        //    }
        //    return retVal;
        //}
        public static int Insert(IncomeDetailsInfo _IncomeDetailsInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_Insert", dbConn);
            
            dbCmd.Parameters.AddWithValue("@IncomeDetails_IncomeID", _IncomeDetailsInfo.IncomeDetails_IncomeID);
            dbCmd.Parameters.AddWithValue("@IncomeDetails_ShipmentID", _IncomeDetailsInfo.IncomeDetails_ShipmentID);
            dbCmd.Parameters.AddWithValue("@IncomeDetails_DebitDays", _IncomeDetailsInfo.IncomeDetails_DebitDays);
            dbCmd.Parameters.AddWithValue("@IncomeDetails_Pay", _IncomeDetailsInfo.IncomeDetails_Pay);
            dbCmd.Parameters.AddWithValue("@IncomeDetails_Instalment", _IncomeDetailsInfo.IncomeDetails_Instalment);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static IncomeDetailsInfo GetInfo(int _IncomeDetails_ID)
        {
            IncomeDetailsInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_IncomeDetails_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@IncomeDetails_ID", _IncomeDetails_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new IncomeDetailsInfo();
                    retVal.IncomeDetails_ID = Convert.ToInt32(dr["IncomeDetails_ID"]);
                    retVal.IncomeDetails_IncomeID = Convert.ToInt32(dr["IncomeDetails_IncomeID"]);
                    retVal.IncomeDetails_ShipmentID = Convert.ToInt32(dr["IncomeDetails_ShipmentID"]);
                    retVal.IncomeDetails_DebitDays = Convert.ToInt32(dr["IncomeDetails_DebitDays"]);
                    retVal.IncomeDetails_Pay = Convert.ToDecimal(dr["IncomeDetails_Pay"]);
                    retVal.IncomeDetails_Instalment = Convert.ToDecimal(dr["IncomeDetails_Instalment"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
